<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      style="position: relative;"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @input.native="filterIcons"
    >
      <i slot="suffix" class="el-icon-search el-input__icon" />
    </el-input>
    <div class="icon-list">
      <div v-for="(item, index) in iconList" :key="index" @click="selectedIcon(item)">
        <i :class="item.font_class" class="custom-i" />
        <span>{{ item.name }}</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'CustomIconIndex',
  props: {
    pc: {
      type: String,
      require: false
    }
  },
  data() {
    return {
      name: '',
      iconList: [],
      oldIconList: []
    }
  },
  created() {
    const customJson = require('@/assets/icons/icon-list/iconfont.json')
    const newObject = customJson.glyphs.map(item => {
      item.font_class = 'iconfont icon-' + item.font_class
      return item
    })
    this.oldIconList = newObject
    this.iconList = this.oldIconList
  },
  methods: {
    filterIcons() {
      this.iconList = this.oldIconList
      if (this.name) {
        this.iconList = this.iconList.filter(item => item.name.includes(this.name))
      }
    },
    selectedIcon(name) {
      this.$emit('selected', name.font_class)
      document.body.click()
    },
    reset() {
      this.name = ''
      this.iconList = this.oldIconList
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.icon-body {
  width: 100%;
  padding: 10px;

  .icon-list {
    height: 200px;
    overflow-y: scroll;
    div {
      height: 60px;
      line-height: 50px;
      margin-bottom: -5px;
      cursor: pointer;
      width: 33%;
      float: left;
    }
    span {
      display: inline-block;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
      width: 100%;
      height: 30px;
      line-height: 30px;
      text-align: center;
    }
  }
}
.custom-i {
  width: 100%;
  height: 30px;
  line-height: 30px;
  float: left;
  font-size: 28px;
  text-align: center;
}
</style>
